<?php
/**
 * 首页相关
 *
 */
class index_model_log extends Model{
	
	/**
	 * 表名
	 */	
	public function __construct(){
		parent::__construct();
		$this->tb_log = 'cartoon_user_log';
	}
	
	/**
	 * 添加用户操作记录
	 *
	 * @param unknown_type $para
	 */
	public function add($args){
		if(!$args['act_type']) $args['act_type'] = 1;
		if(!$args['chapter_id']) $args['chapter_id'] = 0;
		if(!(is_numeric($args['cartoon_id']) && is_numeric($args['uid']))) return false;
		$type = $args['act_type']<=2?'1,2':$args['act_type'];
		$args['up_time'] = CURRENT_TIME;
		$log = $this->getRows($args['uid'],$args['cartoon_id'],$type,$args['chapter_id']);
		if($log){
			if($args['act_type'] == 2 && $log['act_type'] == 1 ){
				$sql = " UPDATE $this->tb_log  SET act_type = 2 WHERE cartoon_id=".$args['cartoon_id']." AND uid=".$args['uid']." AND act_type =1 ";
				$this->db->query($sql);
				Model::factory('cartoon_model_data')->incrField($args['cartoon_id'],'ct_readed');
			}else {
				return -1;//不更新
			}
		}else{
			if($this->db->insert($this->tb_log,$args)){
				if($args['act_type'] <= 2 ){
					$col	=	$args['act_type']==1?'ct_want_read':'ct_readed';
					Model::factory('cartoon_model_data')->incrField($args['cartoon_id'],$col);
				}
				return true;
			}
			return false;
		}
	}
	
	/**
	 * 获取用户的操作的具体记录
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $cartoon_id
	 * @param unknown_type $type
	 * @param unknown_type $chapter_id
	 */
	public function getRows($uid,$cartoon_id,$type = null,$chapter_id = null){
		$sql = "SELECT * FROM $this->tb_log	WHERE cartoon_id=$cartoon_id AND uid=$uid ";
		if($type)	$sql.=" AND act_type IN( $type)";
		if($chapter_id) $sql.=" AND chapter_id = $chapter_id";
		return $this->db->fetchRow($sql);
	}
	
	/**
	 * 确认是否已经已经有操作了
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $cartoon_id
	 * @param unknown_type $type
	 */
	public function chkLog($uid,$cartoon_id,$type){
		
	}
}